<template>
  <el-form
  label-width="80px"
  :model="resourceCategoryForm"
  ref="formRef"
  :rules="formRules">
    <el-form-item label="名称" prop="name">
      <el-input v-model="resourceCategoryForm.name"></el-input>
    </el-form-item>
    <el-form-item label="排序">
      <el-input v-model="resourceCategoryForm.sort"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onSubmit">确认</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getSaveOrderUpdate, getResourceCategory } from '@/services/resource'

export default {
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    categoryId: {
      type: [Number, String]
    }
  },
  data () {
    return {
      resourceCategoryForm: {},
      formRules: {
        name: [
          { required: true, message: '请输入资源分类名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    if (this.isEdit) {
      this.getCategoryList()
    }
  },
  methods: {
    onSubmit () {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        const { data } = await getSaveOrderUpdate(this.resourceCategoryForm)
        if (data.code === '000000') {
          this.$emit('success')
          this.resourceCategoryForm = {}
        }
      })
    },
    onCancel () {
      this.$emit('cancel')
      this.resourceCategoryForm = {}
    },
    // 编辑按钮
    async getCategoryList () {
      const { data } = await getResourceCategory()
      if (data.code === '000000') {
        data.data.map(item => {
          if (this.categoryId === item.id) {
            this.resourceCategoryForm = item
          }
        })
      }
    }
  }
}
</script>

<style lang='scss' scoped></style>
